using System;
using System.Reflection;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Threading;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using ACM.cmsarea08;
using ACM.cmsarea08.mainconfiguration;



namespace ACM.cmsarea08.Core {
	public class UserFormLoader :  IHttpAsyncHandler { //IHttpHandler

		
		public bool IsReusable {
			get { return true; }
		}
			
		

      public void ProcessRequest(System.Web.HttpContext contesto)
        {
			// non usato
        }
		
	  public IAsyncResult BeginProcessRequest(HttpContext ctx, AsyncCallback cb, object obj)
	  {
	    AsyncUserFormLoaderState reqState = new AsyncUserFormLoaderState(ctx, cb, obj);
	    AsyncUserFormLoaderRequest ar = new AsyncUserFormLoaderRequest(reqState);
	    ThreadStart ts = new ThreadStart(ar.ProcessRequest);
	    Thread t = new Thread(ts);
	    t.Start();
	    return reqState;
	  }
        
	  public void EndProcessRequest(IAsyncResult ar)
	  {
	    AsyncUserFormLoaderState ars = ar as AsyncUserFormLoaderState;
	    if (ars != null)
	    {
	     // Clean Up
	    }
	  }      
		
		
	}
	
	public class AsyncUserFormLoaderState : IAsyncResult {
		
		internal HttpContext _ctx;
		internal AsyncCallback _cb;
		internal object _dati;
		private bool _iscompleted = false;
		private ManualResetEvent _callCompleteEvent = null;
		
		public AsyncUserFormLoaderState(HttpContext ctx, AsyncCallback cb, object dati) {
			_ctx = ctx;
			_cb = cb;
			_dati = dati;
			
		}
		internal void CompleteRequest() {
			_iscompleted = true;
			lock(this) {
				if(_callCompleteEvent!=null) {
					_callCompleteEvent.Set();
				}
				 if (_cb != null)
				 	_cb(this);
			}
		}
		public object AsyncState {
			get { return _dati; }
		}
		public bool CompletedSynchronously {
			get { return false; }
		}
		public bool IsCompleted            {
			get { return _iscompleted; }
		}
		public WaitHandle AsyncWaitHandle {
			get {
				lock(this) {
					if(_callCompleteEvent == null) 
						_callCompleteEvent = new ManualResetEvent(false);
					return _callCompleteEvent;
				}
			}
		}



	}
	class AsyncUserFormLoaderRequest
	{
		private ACM.cmsarea08.DBManager.DBClass dclass = new ACM.cmsarea08.DBManager.DBClass();
		private AsyncUserFormLoaderState _asyncRequestState;
	
	  public AsyncUserFormLoaderRequest(AsyncUserFormLoaderState ars)
	  {
	    _asyncRequestState = ars;
	  }
	    
	  public void ProcessRequest()
	  {
			string myform_id = "";
			if(_asyncRequestState._ctx.Request.QueryString["vid"]!=null) {
				myform_id = HttpUtility.UrlDecode(_asyncRequestState._ctx.Request.QueryString["vid"].ToString());
			}
			object _obj = dclass.Standard_SC("select form_xml from "+ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+"utentimodelli where id="+myform_id+";");
			if(_obj ==null) {
				//no view
	    		_asyncRequestState.CompleteRequest();
				return;
			} else {
				ACM.cmsarea08.Function.TextClass tlc = new ACM.cmsarea08.Function.TextClass();
				string myForm = tlc.SqlDecodeBlob(_obj);
				if(myForm!="") {
					LoadForm( HttpUtility.HtmlDecode(myForm.Trim()) );
				} else {
					//no view
				}			
			}

		//finito.
	    _asyncRequestState.CompleteRequest();
	  }
		private void LoadForm(string src) { 
			//va fatta una modifica a form.cs per accettare via interna il caricamento dei dai form. (src e xsl).
			_asyncRequestState._ctx.Response.ContentType = "text/xml";
			_asyncRequestState._ctx.Response.ContentEncoding = System.Text.Encoding.UTF8;
			_asyncRequestState._ctx.Response.Write(src);
			_asyncRequestState._ctx.Response.End();
		}				  
	}
	
	
	
}
